<?php
	//调用配置文件config.php
	require_once 'config.php';
	//连接数据库 
	$conn = mysql_connect($host, $dbusername,$dbpassword);
	if(!$conn){
		die("连接失败".mysql_error());
	}
	//设置访问数据库的编码
	mysql_query("set names utf8",$conn) or die(mysql_errno());
	//选择数据库
	mysql_select_db($dbname,$conn) or die(mysql_errno());

	//如果数据库没有blog,admin,catalogs三个表，则创建
	$sql1 = "create table  if not exists blog(
id int primary key auto_increment,
title varchar(64) not null,
content text not null,
cid int(10),
time timestamp default now()
)default charset=utf8";
	$sql2 = "create table if not exists admin(
id int primary key auto_increment,
name varchar(32) not null,
password varchar(64) not null
)default charset=utf8";
	$sql3 = "create table if not exists catalogs(
cid int primary key auto_increment,
catalog varchar(32) not null
)default charset=utf8";
	mysql_query($sql1, $conn) ;
	mysql_query($sql2, $conn) ;
	mysql_query($sql3, $conn) ;
	
	
	//如果有建表，查询admin表中第一行是否有一行记录,如果没有mysql_num_rows()就会返回0，然后就输入测试数据
	$res = mysql_query("select * from admin limit 0,1", $conn) ;
	if(mysql_num_rows($res)==0){
		$sql1="insert into blog(title, content,cid) value('Hello World','你好，世界',1)";
		$sql2="insert into admin(name, password) value('simbs',md5('simbs'))";
		$sql3="insert into catalogs(catalog) value('linux')";
		$sql4="insert into catalogs(catalog) value('内核')";
		$sql5="insert into catalogs(catalog) value('Gnome')";
		$sql6="insert into catalogs(catalog) value('Qt')";
			
		mysql_query($sql1, $conn) or die(mysql_error()) ;
		mysql_query($sql2, $conn) ;
		mysql_query($sql3, $conn) ;
		mysql_query($sql4, $conn) ;
		mysql_query($sql5, $conn) ;
		mysql_query($sql6, $conn) ;
		
	}
	//不必关闭资源，只需关闭数据库
	mysql_close($conn);

	header('Location: blogManage.php');
?>
